
package com.putao.album.popupwindow;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import com.putao.album.R;
import com.putao.album.child.ActivityChildAdd;
import com.putao.album.child.ActivityChildConnect;
import com.putao.album.share.ShareTools;
import com.putao.album.util.ActivityHelper;

/**
 * Created by yanglun on 15/5/8.
 */
public class PtAddChildPopupWindow implements View.OnClickListener {
    private Context mContext;
    private Activity mActivity;
    private static PopupWindow popupWindow = null;
    private static PtAddChildPopupWindow inst;
    public static ShareTools mShareTools;

    public static PopupWindow Create(Activity activity) {
        if (inst == null) {
            inst = new PtAddChildPopupWindow();
            inst.mContext = activity;
            inst.mActivity = activity;
            inst.initPopuptWindow();
        }
        return popupWindow;
    }

    protected void initPopuptWindow() {
        // TODO Auto-generated method stub

        View popupWindow_view = LayoutInflater.from(mContext).inflate(
                R.layout.layout_home_page_add_child, null, false);
        RelativeLayout root_rl = (RelativeLayout) popupWindow_view.findViewById(R.id.root_rl);
        LinearLayout child_ll = (LinearLayout) popupWindow_view.findViewById(R.id.add_child_ll);
        LinearLayout scan_code_ll = (LinearLayout) popupWindow_view.findViewById(R.id.scan_code_ll);

        popupWindow = new PopupWindow(popupWindow_view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        root_rl.setOnClickListener(this);
        child_ll.setOnClickListener(this);
        scan_code_ll.setOnClickListener(this);
        mShareTools = new ShareTools(mActivity);
        root_rl.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {

                if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_BACK)
                    popupWindow.dismiss();
                return false;
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.add_child_ll:
                Bundle bundle = new Bundle();
                bundle.putBoolean("isSide", true);
                ActivityHelper.startActivity(mActivity, ActivityChildAdd.class, bundle);
                popupWindow.dismiss();
                break;
            case R.id.scan_code_ll:
                ActivityHelper.startActivity(mActivity, ActivityChildConnect.class);
                popupWindow.dismiss();
                break;
            case R.id.root_rl:
                popupWindow.dismiss();
                break;

        }
    }
}